package com.cos.students.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cos.students.bo.LoginBO;
import com.cos.students.common.Const;
import com.cos.students.common.ResponseResult;
import com.cos.students.dao.RcAdminMapper;
import com.cos.students.dao.RcStudentMapper;
import com.cos.students.dao.RcTeacherMapper;
import com.cos.students.entity.RcAdmin;
import com.cos.students.entity.RcStudent;
import com.cos.students.entity.RcTeacher;
import com.cos.students.services.IRcSystemService;
import com.cos.students.util.Base64Utils;
import com.cos.students.vo.input.LoginInputVo;
import com.cos.students.vo.output.LoginInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author: COS
 * @time: 2022/10/25 10:48
 * @description:
 */
@Service
public class IRcSystemServiceImpl implements IRcSystemService {
    @Autowired
    private RcStudentMapper studentMapper;
    @Autowired
    private RcTeacherMapper teacherMapper;
    @Autowired
    private RcAdminMapper adminMapper;
    @Autowired
    private LoginBO loginBO;

    @Override
    public ResponseResult doLogin(LoginInputVo inputVo) {
        //1 登录有3中身份 学生 老师 管理员,有不同的mapper存在
        //判断用户身份 调用不同的mapper 代为较为复杂
        //调用bo处理业务
        LoginInfoVo user = loginBO.getUserByName(inputVo);
        // 非空 密码
        if (user != null && user.getPassword().equals(inputVo.getPassword())) {
            // 判断是不是学生的登录
            if (inputVo.getType()== Const.UserType.STUDENT) {
                // 记录最后登录时间
                studentMapper.updateById(new RcStudent(user.getUserId(), LocalDateTime.now()));
            }
            // 设置 token 
//            String encode = Base64Utils.encode(user.getUsername() + ":qf2204");
//            String s = Base64Utils.decode(encode).split(":")[0];
//            return ResponseResult.createSuccess(encode,user);
            return ResponseResult.createSuccess(user);
        }
        return ResponseResult.createFail("用户名或密码有误");
    }

    @Override
    public RcStudent stuLogin(String username, String password, Integer type) {
        RcStudent stu = studentMapper.selectOne(new QueryWrapper<RcStudent>().eq("student_number", username));
        return stu;
    }
}
